﻿using System.ComponentModel;
using System.ServiceModel.DomainServices.Client;
using System.Collections.ObjectModel;

using demo.Web;
using demo.Web.Services;
using demo.Web.PresentationModel;

namespace demo.ViewModels
{
    public class HomeVM : INotifyPropertyChanged
    {
        private DeptDomainContext ctx = new DeptDomainContext();

        // for Lazy Load, set to false and comment out the [Include] attribute in the PMDepartment class
        private bool EagerLoad = true;

        private ObservableCollection<PMDepartment> _departments;
        public ObservableCollection<PMDepartment> Departments
        {
            get { return _departments; }
            set
            {
                _departments = value;
                NotifyPropertyChanged("Departments");
            }
        }


        private PMDepartment _selectedDepartment;
        public PMDepartment SelectedDepartment
        {
            get { return _selectedDepartment; }
            set
            {
                _selectedDepartment = value;
                NotifyPropertyChanged("SelectedDepartment");
            }
        }


        public void LoadDepts()
        {
            var qry = ctx.GetDepartmentsPMQuery();
            ctx.Load<PMDepartment>(qry, delegate(LoadOperation<PMDepartment> lop)
            {
                ObservableCollection<PMDepartment> depts = new ObservableCollection<PMDepartment>();
                foreach (PMDepartment d in lop.Entities)
                {
                    depts.Add(d);
                }
                this.Departments = depts;
            }
            , null);
        }


        public void LoadEmployeesForDept()
        {
            if (EagerLoad)
                return;

            var qry = ctx.GetPMEmployeesForDeptQuery(this.SelectedDepartment.ID);
            ctx.Load<PMEmployee>(qry, delegate(LoadOperation<PMEmployee> lop)
            {
                NotifyPropertyChanged("SelectedDepartment");
            }
            , null);
        }


        public void AddDept(string name)
        {
            PMDepartment d = new PMDepartment() { Name = name };
            ctx.PMDepartments.Add(d);
            ctx.SubmitChanges(delegate(SubmitOperation so)
            {
                if (!so.HasError)
                {
                    this.Departments.Add(d);
                }

            }, null);
        }


        public void AddEmp(int deptId, string name, string Addr, string Ext)
        {
            PMEmployee emp = new PMEmployee() { Name = name, Addr = Addr, Ext = Ext, DeptID = deptId };
            ctx.PMEmployees.Add(emp);
            ctx.SubmitChanges(delegate(SubmitOperation so)
            {
                if (!so.HasError)
                {
                    NotifyPropertyChanged("SelectedDepartment");
                }
            }, null);

        }



        public void SaveChanges()
        {
            ctx.SubmitChanges();
        }



        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
